#import <UIKit/UIKit.h>
#import <Foundation/Foundation.h>
#import "App.h"

CGImageRef CreateCGImageFromFile(NSString* path)
{
    NSURL*            url = [NSURL fileURLWithPath:path];
    CGImageRef        imageRef = NULL;
    CGImageSourceRef  sourceRef;

    sourceRef = CGImageSourceCreateWithURL((CFURLRef)url, NULL);
    if(sourceRef) {
        imageRef = CGImageSourceCreateImageAtIndex(sourceRef, 0, NULL);
        CFRelease(sourceRef);
    }

    return imageRef;
}

@implementation App


- (void)ShowWaveType:(int)index{
	[_waveTypeView ShowWaveType: index];
	[mainView transition:1 toView:_waveTypeView];
}
- (void)HideWaveType:(int)index{
	[_configView HideWaveType: index];
	[mainView transition:2 toView:_configView];
}
- (void)ShowWaveSpeed:(int)index{
	[_waveSpeedView ShowWaveSpeed: index];
	[mainView transition:1 toView:_waveSpeedView];
}
- (void)HideWaveSpeed:(int)index{
	[_configView HideWaveSpeed: index];
	[mainView transition:2 toView:_configView];
}

- (void)Play:(int)index {
	CGImageRef sourceImage;
	switch(index){
		default:
		case 0 : {
			sourceImage = [[UIImage defaultDesktopImage] imageRef];
			break;
		}
		case 1 : {
			sourceImage = [self CreateImage: @"Bear.jpg"];
			break;
		}
		case 2 : {
			sourceImage = [self CreateImage: @"Bubbles.jpg"];
			break;
		}
		case 3 : {
			sourceImage = [self CreateImage: @"Clownfish.jpg"];
			break;
		}
		case 4 : {
			NSString *imgpath = @"/tmp/foo_0.png";
			sourceImage = CreateCGImageFromFile(imgpath);
			break;
		}
	}
	if (sourceImage != nil){
		[_imageView doinitImage:sourceImage andClean:(index!=0)];
		statusbarheight = [UIHardware statusBarHeight];
		[_imageView startEmulator ];
		[mainView transition: 7 toView: _imageView]; 
		[UIHardware _setStatusBarHeight:0.0f];
		[self setStatusBarMode:2 orientation:0 duration:0.6f fenceID:0];
		[ _window setFrame: [ UIHardware fullScreenApplicationContentRect ] ];
		[ _imageView setFrame: [ UIHardware fullScreenApplicationContentRect ] ];
	}
}

- (void)About {
   [self openURL:[NSURL URLWithString:@"http://Gnese.free.fr/iPhone/Waves/"]];
}

- (void)Donate: (int) currentcy {
	if (currentcy == 1){
		[self openURL:[NSURL URLWithString:@"https://www.paypal.com/cgi-bin/webscr?cmd=_donations&business=fbonnin%40laposte%2enet&item_name=Donation%20for%20iPhone%20free%20software%20developement%20by%20Francis%20Bonnin&no_shipping=1&cn=Comment&tax=0&currency_code=EUR&bn=PP%2dDonationsBF&charset=UTF%2d8"]];
	}
	else {
		[self openURL:[NSURL URLWithString:@"https://www.paypal.com/cgi-bin/webscr?cmd=_donations&business=fbonnin%40laposte%2enet&item_name=Donation%20for%20iPhone%20free%20software%20developement%20by%20Francis%20Bonnin&no_shipping=1&cn=Comment&tax=0&currency_code=USD&bn=PP%2dDonationsBF&charset=UTF%2d8"]];
	}
}
- (CGImageRef) CreateImage: (NSString*) name{
	return CreateCGImageFromFile([NSString stringWithFormat:@"%@/%@", [[NSBundle mainBundle] bundlePath], name] );
}


- (void)Stop {
	[mainView transition: 7 toView: _configView]; 
	[_imageView stopEmulator];
	[UIHardware _setStatusBarHeight:statusbarheight];
	[self setStatusBarMode:0 orientation:0 duration:0.2f fenceID:0];
	[ _window setFrame: [ UIHardware fullScreenApplicationContentRect ] ];

//	[_window setContentView: _configView]; 
//	[_configView prefsFinished];
}


- (void)applicationDidFinishLaunching:(NSNotification *)aNotification {
	_configView = NULL;
	_imageView = NULL;
	_screenOrientation = 0;
//		_snapbutton = [[SnapButton alloc] initWithX: 0.0f Y: 0.0f andParent:self];
	_window = [[UIWindow alloc] initWithContentRect: [UIHardware fullScreenApplicationContentRect]];
		
	struct CGRect rect = [UIHardware fullScreenApplicationContentRect];
	rect.origin.x = rect.origin.y = 0.0f;
	
	mainView = [[UITransitionView alloc] initWithFrame: rect];

	[_window orderFront: self];
	[_window makeKey: self];
	[_window _setHidden: NO];
	[_window setContentView: mainView];

	UIImageView * backgroundView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"Default.png"]];
	[backgroundView setTransform: CGAffineTransformMakeTranslation(0, -[UIHardware statusBarHeight])];
  	[mainView addSubview: backgroundView];

	_imageView = [[MainView alloc] initWithFrame: rect andParent:self];
	screenSurface = [_window createCoreSurfaceWithFrame: CGRectMake(0.0f, 0.0f, 320.0f, 480.0f)];

	_waveTypeView = [[WaveTypeView alloc] initWithFrame: rect andParent:self];
	_waveSpeedView = [[WaveSpeedView alloc] initWithFrame: rect andParent:self];
	_configView = [[ConfigView alloc] initWithFrame: rect andParent:self];

	[mainView transition:8 toView:_configView];
	[backgroundView release];
}





- (void)applicationWillTerminate {
	[_configView Save];
	[_window release];
}


@end
